---
title: "00 Data Cleaning"
author: "Ye Shen, Radhika Tampi, Raj Vatsa"
date: "11/27/2021"
output: pdf_document
---
# Install Required Packages and Dependencies
```{r}
install.packages(c("webshot", "gt", "gtsummary", "tidyverse", "survey", "gridExtra", 
          "grid", "foreign", "MatchIt", "stringr", "WeightIt", "questionr", 
          "patchwork", "cobalt", "optweight", "mlogit", "lme4", "arm", 
          "lmerTest", "optimx", "ggplot2"))

webshot::install_phantomjs()
```

# Preparing Datasets for DiD Extension Analyses
This is the same data cleaning process to produce the DiD analyses used by Ezran et al except that we added some variables (e.g. household ID and wealth index score for all datasets and additionally child birth year for children's data) from the full published dataset to conduct matching and weighting. We saved the cleaned and subsetted data for use in the extensions analyses.

## Children's data
Note that in the last step, we subsetted data to children with any of the three illnesses for the extension analyses in order to obtain maximum sample size for matching v.s. weighting comparisons. i.e. We did not conduct stratified analyses specific to each type of illness. 
```{r}
IHOPE_women_children<-read.csv("data/merged_IHOPE_women_children_2014_2016_v3.csv")
IHOPE_house<-read.csv("data/merged_IHOPE_house_2014_2016_v3.csv")
# Select children born within 5 years of the survey year and who are still alive ("vivant").
# Children who were not born within 5 years are NA in the column "enfant_vivant"
IHOPE_children <- IHOPE_women_children[!is.na(IHOPE_women_children$enfant_vivant), ]
IHOPE_children_2014 <- IHOPE_children[IHOPE_children$questionnaire_menage_annee ==
  2014 & IHOPE_children$enfant_vivant == "vivant", ]
IHOPE_children_2016 <- IHOPE_children[IHOPE_children$questionnaire_menage_annee ==
  2016 & IHOPE_children$enfant_vivant == "vivant", ]

# Variables to calculate for Table 2
variables_children_disease <- c("ID_menage","menage_score_bien_etre","enfant_age_annee",
  "year", "cluster", "group", "wmweight",
  "DIA.disease", "DIA.care", "DIA.location", "DIA.public_level", 
  "DIA.public_healthfacility", "DIA.public_communitycenter",
  "DIA.antibiotic", "DIA.antidiarrheal", "DIA.householdremedy", "DIA.zinc", "DIA.ort",
  "DIA.anytreatment", "DIA.anycontra_treatment",
  "ARI.disease", "ARI.care", "ARI.location", "ARI.public_level",
  "ARI.public_healthfacility", "ARI.public_communitycenter",
  "ARI.antimalarial", "ARI.antibiotic", "ARI.nsaid", "ARI.anytreatment",
  "ARI.anycontra_treatment",
  "FEV.disease", "FEV.care", "FEV.location", "FEV.public_level",
  "FEV.public_healthfacility", "FEV.public_communitycenter",
  "FEV.antimalarial", "FEV.antibiotic", "FEV.nsaid", "FEV.rtd", "FEV.anytreatment",
  "ALL.disease", "ALL.public_level", " ALL.public_healthfacility",
  "ALL.public_communitycenter", "ALL.anytreatment", "ALL.notreatment"
)

children_disease_2014 <- as.data.frame(matrix(nrow = nrow(IHOPE_children_2014), 
                                              ncol = length(variables_children_disease)))
children_disease_2016 <- as.data.frame(matrix(nrow = nrow(IHOPE_children_2016), 
                                              ncol = length(variables_children_disease)))
colnames(children_disease_2014) <- variables_children_disease
colnames(children_disease_2016) <- variables_children_disease

# Binarize every variable
# year - 2014 = 0, 2016 = 1
children_disease_2014$year <- rep(0, nrow(IHOPE_children_2014))
children_disease_2016$year <- rep(1, nrow(IHOPE_children_2016))

# cluster - geographical cluster ("grappe")
children_disease_2014$cluster <- IHOPE_children_2014$grappe
children_disease_2016$cluster <- IHOPE_children_2016$grappe

# ID_menage
children_disease_2014$ID_menage <- IHOPE_children_2014$ID_menage  
children_disease_2016$ID_menage <- IHOPE_children_2016$ID_menage
# group - non-intervention group = 0, intervention group = 1
children_disease_2014$group <- 
  ifelse(IHOPE_children_2014$intervention_group == "intervention_group", 1, 0)
children_disease_2016$group <- 
  ifelse(IHOPE_children_2016$intervention_group == "intervention_group", 1, 0)

# sampling weight - each child's sampling weight is based on mother's sampling weight 
# ("femme_coefficient_ponderation") - to account for the unequal probability of 
# household selection
children_disease_2014$wmweight <- IHOPE_children_2014$femme_coefficient_ponderation
children_disease_2016$wmweight <- IHOPE_children_2016$femme_coefficient_ponderation

#Wealth index
children_disease_2014$menage_score_bien_etre <-IHOPE_children_2014$menage_score_bien_etre
children_disease_2016$menage_score_bien_etre <-IHOPE_children_2016$menage_score_bien_etre

#Children's birth year
children_disease_2014$enfant_age_annee<-IHOPE_children_2014$enfant_age_annee
children_disease_2016$enfant_age_annee<-IHOPE_children_2016$enfant_age_annee

# Acute respiratory infection (ARI)

# ARI.disease - defined as children with a cough ("enfant_toux") and difficulty breathing 
# ("enfant_difficulte_respiratoire") within the last two weeks
children_disease_2014$ARI.disease <- 
  ifelse(IHOPE_children_2014$enfant_toux == "oui" & 
           IHOPE_children_2014$enfant_difficulte_respiratoire == "oui", 1, 0)
children_disease_2016$ARI.disease <- 
  ifelse(IHOPE_children_2016$enfant_toux == "oui" & 
           IHOPE_children_2016$enfant_difficulte_respiratoire == "oui", 1, 0)

# ARI.care - sought care for illness
children_disease_2014$ARI.care <- 
  ifelse(is.na(children_disease_2014$ARI.disease) | 
           children_disease_2014$ARI.disease == 0, NA,
  ifelse(IHOPE_children_2014$enfant_fievre_toux_soins == "oui", 1, 0))
children_disease_2016$ARI.care <- 
  ifelse(is.na(children_disease_2016$ARI.disease) | 
           children_disease_2016$ARI.disease == 0, NA,
  ifelse(IHOPE_children_2016$enfant_fievre_toux_soins == "oui", 1, 0))

# ARI.location - location of care
children_disease_2014$ARI.location <- 
  ifelse(is.na(children_disease_2014$ARI.care) | 
           children_disease_2014$ARI.care == 0, NA,
         as.character(IHOPE_children_2014$enfant_fievre_toux_soins_lieu))
children_disease_2016$ARI.location <- 
  ifelse(is.na(children_disease_2016$ARI.care) | 
           children_disease_2016$ARI.care == 0, NA,
         as.character(IHOPE_children_2016$enfant_fievre_toux_soins_lieu))

# ARI.public_level - sought care at public health facilities (PHFs) 
# or community health worker sites (CHWs)
children_disease_2014$ARI.public_level <- 
  ifelse(is.na(children_disease_2014$ARI.disease) | 
           children_disease_2014$ARI.disease == 0, NA,
  ifelse(children_disease_2014$ARI.care == 1 &
    (children_disease_2014$ARI.location == 
       "centre de sante de base ou hopital publique (PHF)" | 
       children_disease_2014$ARI.location == 
       "agent de sante publique (CHW)"), 1, 0))
children_disease_2016$ARI.public_level <- 
  ifelse(is.na(children_disease_2016$ARI.disease) | 
           children_disease_2016$ARI.disease == 0, NA,
  ifelse(children_disease_2016$ARI.care == 1 &
    (children_disease_2016$ARI.location == 
       "centre de sante de base ou hopital publique (PHF)" | 
       children_disease_2016$ARI.location == 
       "agent de sante publique (CHW)"), 1, 0))

# ARI.public_healthfacility - sought care at public health facilities (PHFs)
children_disease_2014$ARI.public_healthfacility <- 
  ifelse(is.na(children_disease_2014$ARI.disease) | 
           children_disease_2014$ARI.disease == 0, NA,
  ifelse(children_disease_2014$ARI.care == 1 &
    (children_disease_2014$ARI.location == 
       "centre de sante de base ou hopital publique (PHF)"), 1, 0))
children_disease_2016$ARI.public_healthfacility <- 
  ifelse(is.na(children_disease_2016$ARI.disease) | 
           children_disease_2016$ARI.disease == 0, NA,
  ifelse(children_disease_2016$ARI.care == 1 &
    (children_disease_2016$ARI.location == 
       "centre de sante de base ou hopital publique (PHF)"), 1, 0))

# ARI.public_communitycenter - sought care at community health worker sites (CHWs)
children_disease_2014$ARI.public_communitycenter <- 
  ifelse(is.na(children_disease_2014$ARI.disease) | 
           children_disease_2014$ARI.disease == 0, NA,
  ifelse(children_disease_2014$ARI.care == 1 &
    (children_disease_2014$ARI.location == 
       "agent de sante publique (CHW)"), 1, 0))
children_disease_2016$ARI.public_communitycenter <- 
  ifelse(is.na(children_disease_2016$ARI.disease) | 
           children_disease_2016$ARI.disease == 0, NA,
  ifelse(children_disease_2016$ARI.care == 1 &
    (children_disease_2016$ARI.location == 
       "agent de sante publique (CHW)"), 1, 0))

# ARI.antimalarial
children_disease_2014$ARI.antimalarial <- 
  ifelse(is.na(children_disease_2014$ARI.disease) | 
           children_disease_2014$ARI.disease == 0, NA,
  ifelse(children_disease_2014$ARI.public_level == 0, 0,
    ifelse(IHOPE_children_2014$enfant_fievre_toux_traitement_a == "A" | 
             IHOPE_children_2014$enfant_fievre_toux_traitement_b == "B" | 
             IHOPE_children_2014$enfant_fievre_toux_traitement_c == "C" | 
             IHOPE_children_2014$enfant_fievre_toux_traitement_d == "D" | 
             IHOPE_children_2014$enfant_fievre_toux_traitement_e == "E" | 
             IHOPE_children_2014$enfant_fievre_toux_traitement_f == "F", 1, 0)))
children_disease_2016$ARI.antimalarial <- 
  ifelse(is.na(children_disease_2016$ARI.disease) | 
           children_disease_2016$ARI.disease == 0, NA,
  ifelse(children_disease_2016$ARI.public_level == 0, 0,
    ifelse(IHOPE_children_2016$enfant_fievre_toux_traitement_a == "A" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_b == "B" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_c == "C" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_d == "D" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_e == "E" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_f == "F", 1, 0)))

# ARI.antibiotic
children_disease_2014$ARI.antibiotic <- 
  ifelse(is.na(children_disease_2014$ARI.disease) | 
           children_disease_2014$ARI.disease == 0, NA,
  ifelse(children_disease_2014$ARI.public_level == 0, 0,
    ifelse(IHOPE_children_2014$enfant_fievre_toux_traitement_g == "G" | 
             IHOPE_children_2014$enfant_fievre_toux_traitement_h == "H", 1, 0)))
children_disease_2016$ARI.antibiotic <- 
  ifelse(is.na(children_disease_2016$ARI.disease) | 
           children_disease_2016$ARI.disease == 0, NA,
  ifelse(children_disease_2016$ARI.public_level == 0, 0,
    ifelse(IHOPE_children_2016$enfant_fievre_toux_traitement_g == "G" | 
             IHOPE_children_2016$enfant_fievre_toux_traitement_h == "H", 1, 0)))

# ARI.nsaid
children_disease_2014$ARI.nsaid <- 
  ifelse(is.na(children_disease_2014$ARI.disease) | 
           children_disease_2014$ARI.disease == 0, NA,
  ifelse(children_disease_2014$ARI.public_level == 0, 0,
    ifelse(IHOPE_children_2014$enfant_fievre_toux_traitement_i == "I" |
             IHOPE_children_2014$enfant_fievre_toux_traitement_j == "J" |
             IHOPE_children_2014$enfant_fievre_toux_traitement_k == "K" |
             IHOPE_children_2014$enfant_fievre_toux_traitement_l == "L", 1, 0)))
children_disease_2016$ARI.nsaid <- 
  ifelse(is.na(children_disease_2016$ARI.disease) | 
           children_disease_2016$ARI.disease == 0, NA,
  ifelse(children_disease_2016$ARI.public_level == 0, 0,
    ifelse(IHOPE_children_2016$enfant_fievre_toux_traitement_i == "I" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_j == "J" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_k == "K" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_l == "L", 1, 0)))

# ARI.anytreatment
children_disease_2014$ARI.anytreatment <- 
  ifelse(is.na(children_disease_2014$ARI.disease) | 
           children_disease_2014$ARI.disease == 0, NA,
  ifelse(children_disease_2014$ARI.public_level == 0, 0,
    ifelse(children_disease_2014$ARI.antimalarial == 1 |
             children_disease_2014$ARI.antibiotic == 1 | 
             children_disease_2014$ARI.nsaid == 1, 1, 0)))
children_disease_2016$ARI.anytreatment <- 
  ifelse(is.na(children_disease_2016$ARI.disease) | 
           children_disease_2016$ARI.disease == 0, NA,
  ifelse(children_disease_2016$ARI.public_level == 0, 0,
    ifelse(children_disease_2016$ARI.antimalarial == 1 | 
             children_disease_2016$ARI.antibiotic == 1 | 
             children_disease_2016$ARI.nsaid == 1, 1, 0)))

# Diarrheal illness (DIA)

# DIA.disease -  defined as children with diarrhea ("enfant_diarrhee") within the 
# last two weeks
children_disease_2014$DIA.disease <- 
  ifelse(IHOPE_children_2014$enfant_diarrhee == "oui", 1, 0)
children_disease_2016$DIA.disease <- 
  ifelse(IHOPE_children_2016$enfant_diarrhee == "oui", 1, 0)

# DIA.care - sought care for illness
children_disease_2014$DIA.care <- 
  ifelse(is.na(children_disease_2014$DIA.disease) | 
           children_disease_2014$DIA.disease == 0, NA,
  ifelse(IHOPE_children_2014$enfant_diarrhee_soins == "oui", 1, 0))
children_disease_2016$DIA.care <- 
  ifelse(is.na(children_disease_2016$DIA.disease) | 
           children_disease_2016$DIA.disease == 0, NA,
  ifelse(IHOPE_children_2016$enfant_diarrhee_soins == "oui", 1, 0))

# DIA.location - location of care
children_disease_2014$DIA.location <- 
  ifelse(is.na(children_disease_2014$DIA.care) | 
           children_disease_2014$DIA.care == 0, NA, 
         as.character(IHOPE_children_2014$enfant_diarrhee_soins_lieu))
children_disease_2016$DIA.location <- 
  ifelse(is.na(children_disease_2016$DIA.care) | 
           children_disease_2016$DIA.care == 0, NA, 
         as.character(IHOPE_children_2016$enfant_diarrhee_soins_lieu))

# DIA.public_level - sought care at public health facilities (PHFs) or community health 
# worker sites (CHWs)
children_disease_2014$DIA.public_level <- 
  ifelse(is.na(children_disease_2014$DIA.disease) | 
           children_disease_2014$DIA.disease == 0, NA,
  ifelse(children_disease_2014$DIA.care == 1 &
    (children_disease_2014$DIA.location == 
       "centre de sante de base ou hopital publique (PHF)" | 
       children_disease_2014$DIA.location == 
       "agent de sante publique (CHW)"), 1, 0))
children_disease_2016$DIA.public_level <- 
  ifelse(is.na(children_disease_2016$DIA.disease) | 
           children_disease_2016$DIA.disease == 0, NA,
  ifelse(children_disease_2016$DIA.care == 1 &
    (children_disease_2016$DIA.location == 
       "centre de sante de base ou hopital publique (PHF)" | 
       children_disease_2016$DIA.location == 
       "agent de sante publique (CHW)"), 1, 0))

# DIA.public_healthfacility - sought care at public health facilities (PHFs)
children_disease_2014$DIA.public_healthfacility <- 
  ifelse(is.na(children_disease_2014$DIA.disease) | 
           children_disease_2014$DIA.disease == 0, NA,
  ifelse(children_disease_2014$DIA.care == 1 &
    (children_disease_2014$DIA.location == 
       "centre de sante de base ou hopital publique (PHF)"), 1, 0))
children_disease_2016$DIA.public_healthfacility <- 
  ifelse(is.na(children_disease_2016$DIA.disease) | 
           children_disease_2016$DIA.disease == 0, NA,
  ifelse(children_disease_2016$DIA.care == 1 &
    (children_disease_2016$DIA.location == 
       "centre de sante de base ou hopital publique (PHF)"), 1, 0))

# DIA.public_communitycenter - sought care at community health worker sites (CHWs)
children_disease_2014$DIA.public_communitycenter <- 
  ifelse(is.na(children_disease_2014$DIA.disease) | 
           children_disease_2014$DIA.disease == 0, NA,
  ifelse(children_disease_2014$DIA.care == 1 &
    (children_disease_2014$DIA.location == "agent de sante publique (CHW)"), 1, 0))
children_disease_2016$DIA.public_communitycenter <- 
  ifelse(is.na(children_disease_2016$DIA.disease) | 
           children_disease_2016$DIA.disease == 0, NA,
  ifelse(children_disease_2016$DIA.care == 1 &
    (children_disease_2016$DIA.location == "agent de sante publique (CHW)"), 1, 0))

# DIA.antibiotic
children_disease_2014$DIA.antibiotic <- 
  ifelse(is.na(children_disease_2014$DIA.disease) | 
           children_disease_2014$DIA.disease == 0, NA,
  ifelse(children_disease_2014$DIA.public_level == 0, 0,
    ifelse(IHOPE_children_2014$enfant_diarrhee_traitement_a == "A" | 
             IHOPE_children_2014$enfant_diarrhee_traitement_f == "F", 1, 0)))
children_disease_2016$DIA.antibiotic <- 
  ifelse(is.na(children_disease_2016$DIA.disease) | 
           children_disease_2016$DIA.disease == 0, NA,
  ifelse(children_disease_2016$DIA.public_level == 0, 0,
    ifelse(IHOPE_children_2016$enfant_diarrhee_traitement_a == "A" | 
             IHOPE_children_2016$enfant_diarrhee_traitement_f == "F", 1, 0)))

# DIA.antidiarrheal
children_disease_2014$DIA.antidiarrheal <- 
  ifelse(is.na(children_disease_2014$DIA.disease) | 
           children_disease_2014$DIA.disease == 0, NA,
  ifelse(children_disease_2014$DIA.public_level == 0, 0,
    ifelse(IHOPE_children_2014$enfant_diarrhee_traitement_b == "B", 1, 0)))
children_disease_2016$DIA.antidiarrheal <- 
  ifelse(is.na(children_disease_2016$DIA.disease) | 
           children_disease_2016$DIA.disease == 0, NA,
  ifelse(children_disease_2016$DIA.public_level == 0, 0,
    ifelse(IHOPE_children_2016$enfant_diarrhee_traitement_b == "B", 1, 0)))

# DIA.householdremedy
children_disease_2014$DIA.householdremedy <- 
  ifelse(is.na(children_disease_2014$DIA.disease) | 
           children_disease_2014$DIA.disease == 0, NA,
  ifelse(children_disease_2014$DIA.public_level == 0, 0,
    ifelse(IHOPE_children_2014$enfant_diarrhee_traitement_j == "J", 1, 0)))
children_disease_2016$DIA.householdremedy <- 
  ifelse(is.na(children_disease_2016$DIA.disease) | 
           children_disease_2016$DIA.disease == 0, NA,
  ifelse(children_disease_2016$DIA.public_level == 0, 0,
    ifelse(IHOPE_children_2016$enfant_diarrhee_traitement_j == "J", 1, 0)))

# DIA.zinc NEW (include Viasur/Hydrazinc)
children_disease_2014$DIA.zinc <- 
  ifelse(is.na(children_disease_2014$DIA.disease) | 
           children_disease_2014$DIA.disease == 0, NA,
  ifelse(children_disease_2014$DIA.public_level == 0, 0,
    ifelse(IHOPE_children_2014$enfant_diarrhee_traitement_c == "C" | 
             IHOPE_children_2014$enfant_diarrhee_traitement_hydra == "oui", 1, 0)))
children_disease_2016$DIA.zinc <- 
  ifelse(is.na(children_disease_2016$DIA.disease) | 
           children_disease_2016$DIA.disease == 0, NA,
  ifelse(children_disease_2016$DIA.public_level == 0, 0,
    ifelse(IHOPE_children_2016$enfant_diarrhee_traitement_c == "C" | 
             IHOPE_children_2016$enfant_diarrhee_traitement_hydra == "oui", 1, 0)))

# DIA.ort
children_disease_2014$DIA.ort <- 
  ifelse(is.na(children_disease_2014$DIA.disease) | 
           children_disease_2014$DIA.disease == 0, NA,
  ifelse(children_disease_2014$DIA.public_level == 0, 0,
    ifelse(IHOPE_children_2014$enfant_diarrhee_traitement_hydra == "oui" |
             IHOPE_children_2014$enfant_diarrhee_traitement_odiva == "oui" |
             IHOPE_children_2014$enfant_diarrhee_traitement_liqui == "oui", 1, 0)))
children_disease_2016$DIA.ort <- 
  ifelse(is.na(children_disease_2016$DIA.disease) | 
           children_disease_2016$DIA.disease == 0, NA,
  ifelse(children_disease_2016$DIA.public_level == 0, 0,
    ifelse(IHOPE_children_2016$enfant_diarrhee_traitement_hydra == "oui" |
             IHOPE_children_2016$enfant_diarrhee_traitement_odiva == "oui" |
             IHOPE_children_2016$enfant_diarrhee_traitement_liqui == "oui", 1, 0)))

# DIA.anytreatment
children_disease_2014$DIA.anytreatment <- 
  ifelse(is.na(children_disease_2014$DIA.disease) | 
           children_disease_2014$DIA.disease == 0, NA,
  ifelse(children_disease_2014$DIA.public_level == 0, 0,
    ifelse(children_disease_2014$DIA.antibiotic == 1 |
             children_disease_2014$DIA.antidiarrheal == 1 |
             children_disease_2014$DIA.householdremedy == 1 |
      children_disease_2014$DIA.zinc == 1 | 
        children_disease_2014$DIA.ort == 1, 1, 0)))
children_disease_2016$DIA.anytreatment <- 
  ifelse(is.na(children_disease_2016$DIA.disease) | 
           children_disease_2016$DIA.disease == 0, NA,
  ifelse(children_disease_2016$DIA.public_level == 0, 0,
    ifelse(children_disease_2016$DIA.antibiotic == 1 |
             children_disease_2016$DIA.antidiarrheal == 1 |
             children_disease_2016$DIA.householdremedy == 1 |
      children_disease_2016$DIA.zinc == 1 | 
        children_disease_2016$DIA.ort == 1, 1, 0)))


# Febrile illness (FEV)

# FEV.disease - defined as children with fever ("enfant_fievre") within the last two weeks
children_disease_2014$FEV.disease <- 
  ifelse(IHOPE_children_2014$enfant_fievre == "oui", 1, 0)
children_disease_2016$FEV.disease <- 
  ifelse(IHOPE_children_2016$enfant_fievre == "oui", 1, 0)

# FEV.care - sought care for illness
children_disease_2014$FEV.care <- 
  ifelse(is.na(children_disease_2014$FEV.disease) | 
           children_disease_2014$FEV.disease == 0, NA,
  ifelse(IHOPE_children_2014$enfant_fievre_toux_soins == "oui", 1, 0))
children_disease_2016$FEV.care <- 
  ifelse(is.na(children_disease_2016$FEV.disease) | 
           children_disease_2016$FEV.disease == 0, NA,
  ifelse(IHOPE_children_2016$enfant_fievre_toux_soins == "oui", 1, 0))

# FEV.location - location of care
children_disease_2014$FEV.location <- 
  ifelse(is.na(children_disease_2014$FEV.care) | 
           children_disease_2014$FEV.care == 0, NA, 
         as.character(IHOPE_children_2014$enfant_fievre_toux_soins_lieu))
children_disease_2016$FEV.location <- 
  ifelse(is.na(children_disease_2016$FEV.care) | 
           children_disease_2016$FEV.care == 0, NA, 
         as.character(IHOPE_children_2016$enfant_fievre_toux_soins_lieu))

# FEV.public_level - sought care at public health facilities (PHFs) or 
# community health worker sites (CHWs)
children_disease_2014$FEV.public_level <- 
  ifelse(is.na(children_disease_2014$FEV.disease) | 
           children_disease_2014$FEV.disease == 0, NA,
  ifelse(children_disease_2014$FEV.care == 1 &
    (children_disease_2014$FEV.location == 
       "centre de sante de base ou hopital publique (PHF)" | 
       children_disease_2014$FEV.location == 
       "agent de sante publique (CHW)"), 1, 0))
children_disease_2016$FEV.public_level <- 
  ifelse(is.na(children_disease_2016$FEV.disease) | 
           children_disease_2016$FEV.disease == 0, NA,
  ifelse(children_disease_2016$FEV.care == 1 &
    (children_disease_2016$FEV.location == 
       "centre de sante de base ou hopital publique (PHF)" | 
       children_disease_2016$FEV.location == 
       "agent de sante publique (CHW)"), 1, 0))

# FEV.public_healthfacility - sought care at public health facilities (PHFs)
children_disease_2014$FEV.public_healthfacility <- 
  ifelse(is.na(children_disease_2014$FEV.disease) | 
           children_disease_2014$FEV.disease == 0, NA,
  ifelse(children_disease_2014$FEV.care == 1 &
    (children_disease_2014$FEV.location == 
       "centre de sante de base ou hopital publique (PHF)"), 1, 0))
children_disease_2016$FEV.public_healthfacility <- 
  ifelse(is.na(children_disease_2016$FEV.disease) | 
           children_disease_2016$FEV.disease == 0, NA,
  ifelse(children_disease_2016$FEV.care == 1 &
    (children_disease_2016$FEV.location == 
       "centre de sante de base ou hopital publique (PHF)"), 1, 0))

# FEV.public_communitycenter - sought care at community health worker sites (CHWs)
children_disease_2014$FEV.public_communitycenter <- 
  ifelse(is.na(children_disease_2014$FEV.disease) | 
           children_disease_2014$FEV.disease == 0, NA,
  ifelse(children_disease_2014$FEV.care == 1 &
    (children_disease_2014$FEV.location == "agent de sante publique (CHW)"), 1, 0))
children_disease_2016$FEV.public_communitycenter <- 
  ifelse(is.na(children_disease_2016$FEV.disease) | 
           children_disease_2016$FEV.disease == 0, NA,
  ifelse(children_disease_2016$FEV.care == 1 &
    (children_disease_2016$FEV.location == "agent de sante publique (CHW)"), 1, 0))

# FEV.rtd
children_disease_2014$FEV.rtd <- 
  ifelse(is.na(children_disease_2014$FEV.disease) | 
           children_disease_2014$FEV.disease == 0, NA,
  ifelse(children_disease_2014$FEV.public_level == 0, 0,
    ifelse(IHOPE_children_2014$enfant_fievre_sang == "oui", 1, 0)))
children_disease_2016$FEV.rtd <- 
  ifelse(is.na(children_disease_2016$FEV.disease) | 
           children_disease_2016$FEV.disease == 0, NA,
  ifelse(children_disease_2016$FEV.public_level == 0, 0,
    ifelse(IHOPE_children_2016$enfant_fievre_sang == "oui", 1, 0)))

# FEV.antimalarial
children_disease_2014$FEV.antimalarial <- 
  ifelse(is.na(children_disease_2014$FEV.disease) | 
           children_disease_2014$FEV.disease == 0, NA,
  ifelse(children_disease_2014$FEV.public_level == 0, 0,
    ifelse(IHOPE_children_2014$enfant_fievre_toux_traitement_a == "A" |
             IHOPE_children_2014$enfant_fievre_toux_traitement_b == "B" |
             IHOPE_children_2014$enfant_fievre_toux_traitement_c == "C" |
             IHOPE_children_2014$enfant_fievre_toux_traitement_d == "D" |
             IHOPE_children_2014$enfant_fievre_toux_traitement_e == "E" |
             IHOPE_children_2014$enfant_fievre_toux_traitement_f == "F", 1, 0)))
children_disease_2016$FEV.antimalarial <- 
  ifelse(is.na(children_disease_2016$FEV.disease) | 
           children_disease_2016$FEV.disease == 0, NA,
  ifelse(children_disease_2016$FEV.public_level == 0, 0,
    ifelse(IHOPE_children_2016$enfant_fievre_toux_traitement_a == "A" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_b == "B" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_c == "C" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_d == "D" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_e == "E" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_f == "F", 1, 0)))

# FEV.antibiotic
children_disease_2014$FEV.antibiotic <- 
  ifelse(is.na(children_disease_2014$FEV.disease) | 
           children_disease_2014$FEV.disease == 0, NA,
  ifelse(children_disease_2014$FEV.public_level == 0, 0,
    ifelse(IHOPE_children_2014$enfant_fievre_toux_traitement_g == "G" |
             IHOPE_children_2014$enfant_fievre_toux_traitement_h == "H", 1, 0)))
children_disease_2016$FEV.antibiotic <- 
  ifelse(is.na(children_disease_2016$FEV.disease) | 
           children_disease_2016$FEV.disease == 0, NA,
  ifelse(children_disease_2016$FEV.public_level == 0, 0,
    ifelse(IHOPE_children_2016$enfant_fievre_toux_traitement_g == "G" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_h == "H", 1, 0)))

# FEV.nsaid
children_disease_2014$FEV.nsaid <- 
  ifelse(is.na(children_disease_2014$FEV.disease) | 
           children_disease_2014$FEV.disease == 0, NA,
  ifelse(children_disease_2014$FEV.public_level == 0, 0,
    ifelse(IHOPE_children_2014$enfant_fievre_toux_traitement_i == "I" |
             IHOPE_children_2014$enfant_fievre_toux_traitement_j == "J" |
             IHOPE_children_2014$enfant_fievre_toux_traitement_k == "K" |
             IHOPE_children_2014$enfant_fievre_toux_traitement_l == "L", 1, 0)))
children_disease_2016$FEV.nsaid <- 
  ifelse(is.na(children_disease_2016$FEV.disease) | 
           children_disease_2016$FEV.disease == 0, NA,
  ifelse(children_disease_2016$FEV.public_level == 0, 0,
    ifelse(IHOPE_children_2016$enfant_fievre_toux_traitement_i == "I" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_j == "J" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_k == "K" |
             IHOPE_children_2016$enfant_fievre_toux_traitement_l == "L", 1, 0)))

# any.treatment
children_disease_2014$FEV.anytreatment <- 
  ifelse(is.na(children_disease_2014$FEV.disease) | 
           children_disease_2014$FEV.disease == 0, NA,
  ifelse(children_disease_2014$FEV.public_level == 0, 0,
    ifelse(children_disease_2014$FEV.antimalarial == 1 | 
             children_disease_2014$FEV.antibiotic == 1 | 
             children_disease_2014$FEV.nsaid == 1, 1, 0)))
children_disease_2016$FEV.anytreatment <- 
  ifelse(is.na(children_disease_2016$FEV.disease) | 
           children_disease_2016$FEV.disease == 0, NA,
  ifelse(children_disease_2016$FEV.public_level == 0, 0,
    ifelse(children_disease_2016$FEV.antimalarial == 1 | 
             children_disease_2016$FEV.antibiotic == 1 | 
             children_disease_2016$FEV.nsaid == 1, 1, 0)))

# All illnesses
# ALL.disease - Children who either had diarrhea, cough/difficulty breathing, 
# or fever within the last two weeks
children_disease_2014$ALL.disease <- 
  ifelse(is.na(children_disease_2014$DIA.disease), NA,
  ifelse(children_disease_2014$DIA.disease == 1 | 
           children_disease_2014$ARI.disease == 1 | 
           children_disease_2014$FEV.disease == 1, 1, 0))
children_disease_2016$ALL.disease <- 
  ifelse(is.na(children_disease_2016$DIA.disease), NA,
  ifelse(children_disease_2016$DIA.disease == 1 | 
           children_disease_2016$ARI.disease == 1 | 
           children_disease_2016$FEV.disease == 1, 1, 0))

# ALL.public_level - sought care at public health facilities (PHFs) or 
# community health worker sites (CHWs)
for (i in 1:nrow(children_disease_2014)) {
  if (is.na(children_disease_2014$ALL.disease[i]) | 
      children_disease_2014$ALL.disease[i] == 0) {
    children_disease_2014$ALL.public_level[i] <- NA
  } else {
    xx <- sum(children_disease_2014$DIA.public_level[i], 
              children_disease_2014$ARI.public_level[i], 
              children_disease_2014$FEV.public_level[i], na.rm = T)
    children_disease_2014$ALL.public_level[i] <- ifelse(xx > 0, 1, 0)
  }
}
for (i in 1:nrow(children_disease_2016)) {
  if (is.na(children_disease_2016$ALL.disease[i]) | 
      children_disease_2016$ALL.disease[i] == 0) {
    children_disease_2016$ALL.public_level[i] <- NA
  } else {
    xx <- sum(children_disease_2016$DIA.public_level[i], 
              children_disease_2016$ARI.public_level[i], 
              children_disease_2016$FEV.public_level[i], na.rm = T)
    children_disease_2016$ALL.public_level[i] <- ifelse(xx > 0, 1, 0)
  }
}

# ALL.public_healthfacility - sought care at public health facilities (PHFs)
for (i in 1:nrow(children_disease_2014)) {
  if (is.na(children_disease_2014$ALL.disease[i]) | 
      children_disease_2014$ALL.disease[i] == 0) {
    children_disease_2014$ALL.public_healthfacility[i] <- NA
  } else {
    xx <- sum(children_disease_2014$DIA.public_healthfacility[i], 
              children_disease_2014$ARI.public_healthfacility[i], 
              children_disease_2014$FEV.public_healthfacility[i], na.rm = T)
    children_disease_2014$ALL.public_healthfacility[i] <- ifelse(xx > 0, 1, 0)
  }
}
for (i in 1:nrow(children_disease_2016)) {
  if (is.na(children_disease_2016$ALL.disease[i]) | 
      children_disease_2016$ALL.disease[i] == 0) {
    children_disease_2016$ALL.public_level[i] <- NA
  } else {
    xx <- sum(children_disease_2016$DIA.public_level[i], 
              children_disease_2016$ARI.public_level[i], 
              children_disease_2016$FEV.public_level[i], na.rm = T)
    children_disease_2016$ALL.public_level[i] <- ifelse(xx > 0, 1, 0)
  }
}
for (i in 1:nrow(children_disease_2016)) {
  if (is.na(children_disease_2016$ALL.disease[i]) | 
      children_disease_2016$ALL.disease[i] == 0) {
    children_disease_2016$ALL.public_healthfacility[i] <- NA
  } else {
    xx <- sum(children_disease_2016$DIA.public_healthfacility[i], 
              children_disease_2016$ARI.public_healthfacility[i], 
              children_disease_2016$FEV.public_healthfacility[i], na.rm = T)
    children_disease_2016$ALL.public_healthfacility[i] <- ifelse(xx > 0, 1, 0)
  }
}

# ALL.public_communitycenter - sought care at community health worker sites (CHWs)
for (i in 1:nrow(children_disease_2014)) {
  if (is.na(children_disease_2014$ALL.disease[i]) | 
      children_disease_2014$ALL.disease[i] == 0) {
    children_disease_2014$ALL.public_communitycenter[i] <- NA
  } else {
    xx <- sum(children_disease_2014$DIA.public_communitycenter[i], 
              children_disease_2014$ARI.public_communitycenter[i], 
              children_disease_2014$FEV.public_communitycenter[i], na.rm = T)
    children_disease_2014$ALL.public_communitycenter[i] <- ifelse(xx > 0, 1, 0)
  }
}
for (i in 1:nrow(children_disease_2016)) {
  if (is.na(children_disease_2016$ALL.disease[i]) | 
      children_disease_2016$ALL.disease[i] == 0) {
    children_disease_2016$ALL.public_communitycenter[i] <- NA
  } else {
    xx <- sum(children_disease_2016$DIA.public_communitycenter[i],
              children_disease_2016$ARI.public_communitycenter[i],
              children_disease_2016$FEV.public_communitycenter[i], na.rm = T)
    children_disease_2016$ALL.public_communitycenter[i] <- ifelse(xx > 0, 1, 0)
  }
}

# ALL.anytreatment - received any treatment
for (i in 1:nrow(children_disease_2014)) {
  if (is.na(children_disease_2014$ALL.disease[i]) | 
      children_disease_2014$ALL.disease[i] == 0) {
    children_disease_2014$ALL.anytreatment[i] <- NA
  } else {
    xx <- sum(children_disease_2014$DIA.anytreatment[i], 
              children_disease_2014$ARI.anytreatment[i], 
              children_disease_2014$FEV.anytreatment[i], na.rm = T)
    children_disease_2014$ALL.anytreatment[i] <- ifelse(xx > 0, 1, 0)
  }
}
for (i in 1:nrow(children_disease_2016)) {
  if (is.na(children_disease_2016$ALL.disease[i]) | 
      children_disease_2016$ALL.disease[i] == 0) {
    children_disease_2016$ALL.anytreatment[i] <- NA
  } else {
    xx <- sum(children_disease_2016$DIA.anytreatment[i], 
              children_disease_2016$ARI.anytreatment[i], 
              children_disease_2016$FEV.anytreatment[i], na.rm = T)
    children_disease_2016$ALL.anytreatment[i] <- ifelse(xx > 0, 1, 0)
  }
}

# Combined 2014, 2016 data fame
children_disease_did <- rbind(children_disease_2014, children_disease_2016)

#Filter to children with any symptoms only (i.e. All.disease==1) for extension analyses
children_disease_did_alldisease <-subset(children_disease_did, children_disease_did$ALL.disease ==1)

saveRDS(children_disease_did_alldisease,"data/cleaned_children_did_alldis.rds")
```

# Women's Data for Antenatal Outcomes 
```{r}
IHOPE_women_children<-read.csv("data/merged_IHOPE_women_children_2014_2016_v3.csv")
IHOPE_house<-read.csv("data/merged_IHOPE_house_2014_2016_v3.csv")
##antenatal care in women---------------

#Select women whose last child was born within two years of survey
#"questionnaire_menage_annee" - year of survey; "enfant_age_annee" - year child was born; "enfant_age_mois" - month child was born; "consultation_prenat_enfant" - received/did not receive antenatal services
IHOPE_antenatal_2014 = IHOPE_women_children[IHOPE_women_children$questionnaire_menage_annee==2014,]
IHOPE_antenatal_2016 = IHOPE_women_children[IHOPE_women_children$questionnaire_menage_annee==2016,]

#2014 survey was conducted between 4/2014 and 5/2014 so children's age depends on the month in which the survey was conducted. Kept children who were 24 months old at 3/2014 or younger
IHOPE_antenatal_2014<-IHOPE_antenatal_2014[which(is.na(IHOPE_antenatal_2014$consultation_prenat_enfant)==FALSE & IHOPE_antenatal_2014$enfant_age_annee>=2012),]
IHOPE_antenatal_2014<-IHOPE_antenatal_2014[-which(IHOPE_antenatal_2014$enfant_age_annee==2012 & IHOPE_antenatal_2014$enfant_age_mois<4),]
#2016 survey was conducted between 8/2016 and 9/2016 so children's age depends on the month in which the survey was conducted. Kept children who were 24 months old at 7/2016 or younger
IHOPE_antenatal_2016<-IHOPE_antenatal_2016[which(is.na(IHOPE_antenatal_2016$consultation_prenat_enfant)==FALSE & IHOPE_antenatal_2016$enfant_age_annee>=2014),]
IHOPE_antenatal_2016<-IHOPE_antenatal_2016[-which(IHOPE_antenatal_2016$enfant_age_annee==2014 & IHOPE_antenatal_2016$enfant_age_mois<8),]

#Variables to calculate for Table 3 antenatal care----------------

variables_women_antenatal<-c('ID_menage','year','cluster','group','wmweight',
                             'menage_score_bien_etre',
                             'antenatal.care', 'antenatal.location',
                             'antenatal.healthcenter.consultation',
                             'antenatal.publichealthcenter.consultation',
                             'antenatal.month.firstconsultation',
                             'antenatal.firsttrimester.firstconsultation',
                             'antenatal.number.consultations',
                             'antenatal.fourORmore.consultations',
                             'antenatal.weight','antenatal.bloodpressure',
                             'antenatal.bloodtest','antenatal.urinetest',
                             'antenatal.complications.explained',
                             'antenatal.complications.location')


women_antenatal_2014<-as.data.frame(matrix(nrow=nrow(IHOPE_antenatal_2014), ncol=length(variables_women_antenatal)))
women_antenatal_2016<-as.data.frame(matrix(nrow=nrow(IHOPE_antenatal_2016), ncol=length(variables_women_antenatal)))

colnames(women_antenatal_2014)<-variables_women_antenatal
colnames(women_antenatal_2016)<-variables_women_antenatal

#Binarize every variable ------------------
#year - 2014 = 0, 2016 = 1
women_antenatal_2014$year<-rep(0, nrow(IHOPE_antenatal_2014))
women_antenatal_2016$year<-rep(1, nrow(IHOPE_antenatal_2016))

#cluster - geographical cluster ("grappe")
women_antenatal_2014$cluster<-IHOPE_antenatal_2014$grappe
women_antenatal_2016$cluster<-IHOPE_antenatal_2016$grappe

#group - non-intervention group =0, intervention group =1
women_antenatal_2014$group<-ifelse(IHOPE_antenatal_2014$intervention_group=="intervention_group",1, 0)
women_antenatal_2016$group<-ifelse(IHOPE_antenatal_2016$intervention_group=="intervention_group",1, 0)
# ID_menage
women_antenatal_2014$ID_menage <- IHOPE_antenatal_2014$ID_menage  
women_antenatal_2016$ID_menage <- IHOPE_antenatal_2016$ID_menage

#sampling weight ("femme_coefficient_ponderation") - to account for the unequal probability of household selection
women_antenatal_2014$wmweight<-IHOPE_antenatal_2014$femme_coefficient_ponderation
women_antenatal_2016$wmweight<-IHOPE_antenatal_2016$femme_coefficient_ponderation

#Wealth index
women_antenatal_2014$menage_score_bien_etre <-IHOPE_antenatal_2014$menage_score_bien_etre
women_antenatal_2016$menage_score_bien_etre <-IHOPE_antenatal_2016$menage_score_bien_etre

#antenatal.care - received antenatal care ("consultation_prenat_enfant"): "oui" = yes, "non" = no
women_antenatal_2014$antenatal.care<-ifelse(IHOPE_antenatal_2014$consultation_prenat_enfant=="oui",1,0)
women_antenatal_2016$antenatal.care<-ifelse(IHOPE_antenatal_2016$consultation_prenat_enfant=="oui",1,0)

#antenatal.location - location of antenatal care received ("consultation_prenat_lieu")
women_antenatal_2014$antenatal.location<-ifelse(women_antenatal_2014$antenatal.care==0,0,
                                           ifelse(IHOPE_antenatal_2014$consultation_prenat_lieu=="centre de sante de base ou hopital publique (PHF)","Public sector",
                                                  ifelse(IHOPE_antenatal_2014$consultation_prenat_lieu=="maison","House sector",
                                                         ifelse(IHOPE_antenatal_2014$consultation_prenat_lieu=="centre de sante ou hopital prive", "Private sector",
                                                                "Other"))))
women_antenatal_2016$antenatal.location<-ifelse(women_antenatal_2016$antenatal.care==0,0,
                                            ifelse(IHOPE_antenatal_2016$consultation_prenat_lieu=="centre de sante de base ou hopital publique (PHF)","Public sector",
                                                   ifelse(IHOPE_antenatal_2016$consultation_prenat_lieu=="maison","House sector",
                                                          ifelse(IHOPE_antenatal_2016$consultation_prenat_lieu=="centre de sante ou hopital prive", "Private sector",
                                                                 "Other"))))

#antenatal.healthcenter.consultation - received antenatal care at a health center (public or private)
women_antenatal_2014$antenatal.healthcenter.consultation<-ifelse(women_antenatal_2014$antenatal.care==0,0,
                                                ifelse(women_antenatal_2014$antenatal.location=="Public sector"|women_antenatal_2014$antenatal.location=="Private sector",1,0))
women_antenatal_2016$antenatal.healthcenter.consultation<-ifelse(women_antenatal_2016$antenatal.care==0,0,
                                                             ifelse(women_antenatal_2016$antenatal.location=="Public sector"|women_antenatal_2016$antenatal.location=="Private sector",1,0))

#antenatal.publichealthcenter.consultation - received antenatal care at a public health center
women_antenatal_2014$antenatal.publichealthcenter.consultation<-ifelse(women_antenatal_2014$antenatal.care==0,0,
                                                      ifelse(women_antenatal_2014$antenatal.location=="Public sector" ,1,0))
women_antenatal_2016$antenatal.publichealthcenter.consultation<-ifelse(women_antenatal_2016$antenatal.care==0,0,
                                                                   ifelse(women_antenatal_2016$antenatal.location=="Public sector" ,1,0))

#antenatal.month.firstconsultation - number of months pregnant when mother had first antenatal visit ("consultation_prenat_mois")
women_antenatal_2014$antenatal.month.firstconsultation<-ifelse(women_antenatal_2014$antenatal.publichealthcenter.consultation==0,0,IHOPE_antenatal_2014$consultation_prenat_mois)
women_antenatal_2016$antenatal.month.firstconsultation<-ifelse(women_antenatal_2016$antenatal.publichealthcenter.consultation==0,0,IHOPE_antenatal_2016$consultation_prenat_mois)

#antenatal.firsttrimester.firstconsultation - first antenatal visit within first trimester
women_antenatal_2014$antenatal.firsttrimester.firstconsultation<-ifelse(women_antenatal_2014$antenatal.publichealthcenter.consultation==0,0,
                                                              ifelse(women_antenatal_2014$antenatal.month.firstconsultation<=3,1,0))
women_antenatal_2016$antenatal.firsttrimester.firstconsultation<-ifelse(women_antenatal_2016$antenatal.publichealthcenter.consultation==0,0,
                                                                    ifelse(women_antenatal_2016$antenatal.month.firstconsultation<=3,1,0))

#antenatal.number.consultations - total number of antenatal visits during pregnancy ("consultation_prenat_frequence")
women_antenatal_2014$antenatal.number.consultations<-ifelse(women_antenatal_2014$antenatal.publichealthcenter.consultation==0,0,IHOPE_antenatal_2014$consultation_prenat_frequence)
women_antenatal_2016$antenatal.number.consultations<-ifelse(women_antenatal_2016$antenatal.publichealthcenter.consultation==0,0,IHOPE_antenatal_2016$consultation_prenat_frequence)

#antenatal.fourORmore.consultations - received 4 or more antenatal visits during pregnancy
women_antenatal_2014$antenatal.fourORmore.consultations<-ifelse(women_antenatal_2014$antenatal.publichealthcenter.consultation==0,0,
                                                             ifelse(women_antenatal_2014$antenatal.number.consultations>=4,1,0))
women_antenatal_2016$antenatal.fourORmore.consultations<-ifelse(women_antenatal_2016$antenatal.publichealthcenter.consultation==0,0,
                                                            ifelse(women_antenatal_2016$antenatal.number.consultations>=4,1,0))

#antenatal.weight - weight was obtained during at least one antenatal visit ("consultation_prenat_poids")
women_antenatal_2014$antenatal.weight<-ifelse(women_antenatal_2014$antenatal.publichealthcenter.consultation==0,0,
                                         ifelse(IHOPE_antenatal_2014$consultation_prenat_poids=="oui",1,0))
women_antenatal_2016$antenatal.weight<-ifelse(women_antenatal_2016$antenatal.publichealthcenter.consultation==0,0,
                                          ifelse(IHOPE_antenatal_2016$consultation_prenat_poids=="oui",1,0))

#antenatal.bloodpressure -  blood pressure was obtained during at least one antenatal visit ("consultation_prenat_tension")
women_antenatal_2014$antenatal.bloodpressure<-ifelse(women_antenatal_2014$antenatal.publichealthcenter.consultation==0,0,
                                         ifelse(IHOPE_antenatal_2014$consultation_prenat_tension=="oui",1,0))
women_antenatal_2016$antenatal.bloodpressure<-ifelse(women_antenatal_2016$antenatal.publichealthcenter.consultation==0,0,
                                                 ifelse(IHOPE_antenatal_2016$consultation_prenat_tension=="oui",1,0))

#antenatal.bloodtest -  blood test was obtained during at least one antenatal visit ("consultation_prenat_sang")
women_antenatal_2014$antenatal.bloodtest<-ifelse(women_antenatal_2014$antenatal.publichealthcenter.consultation==0,0,
                                         ifelse(IHOPE_antenatal_2014$consultation_prenat_sang=="oui",1,0))
women_antenatal_2016$antenatal.bloodtest<-ifelse(women_antenatal_2016$antenatal.publichealthcenter.consultation==0,0,
                                             ifelse(IHOPE_antenatal_2016$consultation_prenat_sang=="oui",1,0))

#antenatal.urinetest -  urine analysis was obtained during at least one antenatal visit ("consultation_prenat_urine")
women_antenatal_2014$antenatal.urinetest<-ifelse(women_antenatal_2014$antenatal.publichealthcenter.consultation==0,0,
                                         ifelse(IHOPE_antenatal_2014$consultation_prenat_urine=="oui",1,0))
women_antenatal_2016$antenatal.urinetest<-ifelse(women_antenatal_2016$antenatal.publichealthcenter.consultation==0,0,
                                             ifelse(IHOPE_antenatal_2016$consultation_prenat_urine=="oui",1,0))

#antenatal.complications.explained - counseled at least once about potential pregnancy complications
women_antenatal_2014$antenatal.complications.explained<-ifelse(women_antenatal_2014$antenatal.publichealthcenter.consultation==0,0,
                                            ifelse(IHOPE_antenatal_2014$consultation_prenat_complicat_a=="oui",1,0))
women_antenatal_2016$antenatal.complications.explained<-ifelse(women_antenatal_2016$antenatal.publichealthcenter.consultation==0,0,
                                                           ifelse(IHOPE_antenatal_2016$consultation_prenat_complicat_a=="oui",1,0))

#antenatal.complications.location -  counseled at least once about where to go if woman develops pregnancy complications (asked only if antenatal.complications.explained =1)
women_antenatal_2014$antenatal.complications.location<-ifelse(women_antenatal_2014$antenatal.publichealthcenter.consultation==0,0,
                                                      ifelse(women_antenatal_2014$antenatal.complications.explained==0,0,
                                                         ifelse(IHOPE_antenatal_2014$consultation_prenat_complicat_b=="oui",1,0)))
women_antenatal_2016$antenatal.complications.location<-ifelse(women_antenatal_2016$antenatal.publichealthcenter.consultation==0,0,
                                                          ifelse(women_antenatal_2016$antenatal.complications.explained==0,0,
                                                                 ifelse(IHOPE_antenatal_2016$consultation_prenat_complicat_b=="oui",1,0)))



#Combined 2014 2016 datafame---------------
women_antenatal_did=rbind(women_antenatal_2014,women_antenatal_2016)
saveRDS(women_antenatal_did,"data/cleaned_antenatal_did.rds")

```
# Women's Data for Perinatal Outcomes
```{r}
IHOPE_women_children<-read.csv("data/merged_IHOPE_women_children_2014_2016_v3.csv")
IHOPE_house<-read.csv("data/merged_IHOPE_house_2014_2016_v3.csv")
#Select women whose last child was born within two years of survey
#"questionnaire_menage_annee" - year of survey; "enfant_age_annee" - year child was born; "enfant_age_mois" - month child was born; "consultation_prenat_enfant" - received/did not receive prenatal services
IHOPE_perinatal_2014 = IHOPE_women_children[IHOPE_women_children$questionnaire_menage_annee==2014,]
IHOPE_perinatal_2016 = IHOPE_women_children[IHOPE_women_children$questionnaire_menage_annee==2016,]

#2014 survey was conducted between 4/2014 and 5/2014 so children's age depends on the month in which the survey was conducted. Kept children who were 24 months old at 3/2014 or younger
IHOPE_perinatal_2014<-IHOPE_perinatal_2014[which(is.na(IHOPE_perinatal_2014$consultation_prenat_enfant)==FALSE & IHOPE_perinatal_2014$enfant_age_annee>=2012),]
IHOPE_perinatal_2014<-IHOPE_perinatal_2014[-which(IHOPE_perinatal_2014$enfant_age_annee==2012 & IHOPE_perinatal_2014$enfant_age_mois<4),]
#2016 survey was conducted between 8/2016 and 9/2016 so children's age depends on the month in which the survey was conducted. Kept children who were 24 months old at 7/2016 or younger
IHOPE_perinatal_2016<-IHOPE_perinatal_2016[which(is.na(IHOPE_perinatal_2016$consultation_prenat_enfant)==FALSE & IHOPE_perinatal_2016$enfant_age_annee>=2014),]
IHOPE_perinatal_2016<-IHOPE_perinatal_2016[-which(IHOPE_perinatal_2016$enfant_age_annee==2014 & IHOPE_perinatal_2016$enfant_age_mois<8),]

#Variables to calculate for Table 3 perinatal care----------------

variables_women_perinatal<-c('ID_menage','year','cluster', 'group','wmweight', "allones",
                             'prenat.care','menage_score_bien_etre',
                             'durmat.delivery.location', 'durmat.healthcenter.delivery','durmat.publichealthcenter.delivery', 
                             'durmat.healthcareprofessional.assisted',
                             
                             'durmat.mother.health.checked', 'durmat.mother.health.checked.professional',
                             'durmat.6hourORless.mother.health.checked','durmat.6hourORless.mother.health.checked.professional',
                             'durmat.baby.weighed','durmat.baby.breastfed','durmat.baby.1hourORless.breastfed',
                             
                             'pstmat.baby.health.checked', 'pstmat.baby.health.checked.professional',
                             'pstmat.6hourORless.baby.health.checked','pstmat.6hourORless.baby.health.checked.professional',
                             'pstmat.baby.noliquids','pstmat.baby.alive')


women_perinatal_2014<-as.data.frame(matrix(nrow=nrow(IHOPE_perinatal_2014), ncol=length(variables_women_perinatal)))
women_perinatal_2016<-as.data.frame(matrix(nrow=nrow(IHOPE_perinatal_2016), ncol=length(variables_women_perinatal)))

colnames(women_perinatal_2014)<-variables_women_perinatal
colnames(women_perinatal_2016)<-variables_women_perinatal



#Binarize every variable ------------------
# ID_menage
women_perinatal_2014$ID_menage <- IHOPE_perinatal_2014$ID_menage  
women_perinatal_2016$ID_menage <- IHOPE_perinatal_2016$ID_menage


#year - 2014 = 0, 2016 = 1
women_perinatal_2014$year<-rep(0, nrow(IHOPE_perinatal_2014))
women_perinatal_2016$year<-rep(1, nrow(IHOPE_perinatal_2016))

#cluster - geographical cluster ("grappe")
women_perinatal_2014$cluster<-IHOPE_perinatal_2014$grappe
women_perinatal_2016$cluster<-IHOPE_perinatal_2016$grappe

#group - non-intervention group =0, intervention group =1
women_perinatal_2014$group<-ifelse(IHOPE_perinatal_2014$intervention_group=="intervention_group",1, 0)
women_perinatal_2016$group<-ifelse(IHOPE_perinatal_2016$intervention_group=="intervention_group",1, 0)

#sampling weight ("femme_coefficient_ponderation") - to account for the unequal probability of household selection
women_perinatal_2014$wmweight<-IHOPE_perinatal_2014$femme_coefficient_ponderation
women_perinatal_2016$wmweight<-IHOPE_perinatal_2016$femme_coefficient_ponderation

#allones
women_perinatal_2014$allones<-rep(1,nrow(IHOPE_perinatal_2014))
women_perinatal_2016$allones<-rep(1,nrow(IHOPE_perinatal_2016))

#Wealth index
women_perinatal_2014$menage_score_bien_etre <-IHOPE_perinatal_2014$menage_score_bien_etre
women_perinatal_2016$menage_score_bien_etre <-IHOPE_perinatal_2016$menage_score_bien_etre

#prenat.care - received prenatal care ("consultation_prenat_enfant"): "oui" = yes, "non" = no
women_perinatal_2014$prenat.care<-ifelse(IHOPE_perinatal_2014$consultation_prenat_enfant=="oui",1,0)
women_perinatal_2016$prenat.care<-ifelse(IHOPE_perinatal_2016$consultation_prenat_enfant=="oui",1,0)

#durmat.delivery.location - location of delivery ("accouchement_lieu"). Note that no woman delivered in a private hospital
women_perinatal_2014$durmat.delivery.location<-ifelse(IHOPE_perinatal_2014$accouchement_lieu=="maison","House sector",
                                                    ifelse(IHOPE_perinatal_2014$accouchement_lieu=="centre de sante de base ou hopital publique (PHF)","Public sector",
                                                           ifelse(IHOPE_perinatal_2014$accouchement_lieu=="  centre de sante ou hopital prive","Private sector",
                                                                  "Other sector")))
women_perinatal_2016$durmat.delivery.location<-ifelse(IHOPE_perinatal_2016$accouchement_lieu=="maison","House sector",
                                                      ifelse(IHOPE_perinatal_2016$accouchement_lieu=="centre de sante de base ou hopital publique (PHF)","Public sector",
                                                             ifelse(IHOPE_perinatal_2016$accouchement_lieu=="  centre de sante ou hopital prive","Private sector",
                                                                    "Other sector")))

#durmat.healthcenter.delivery - delivered at a health center (public or private)
women_perinatal_2014$durmat.healthcenter.delivery<-ifelse(women_perinatal_2014$durmat.delivery.location=="Public sector"| women_perinatal_2014$durmat.delivery.location=="Private sector",1,0)
women_perinatal_2016$durmat.healthcenter.delivery<-ifelse(women_perinatal_2016$durmat.delivery.location=="Public sector"| women_perinatal_2016$durmat.delivery.location=="Private sector",1,0)

#durmat.publichealthcenter.delivery  - delivered at a public health center
women_perinatal_2014$durmat.publichealthcenter.delivery<-ifelse(women_perinatal_2014$durmat.delivery.location=="Public sector",1,0)
women_perinatal_2016$durmat.publichealthcenter.delivery<-ifelse(women_perinatal_2016$durmat.delivery.location=="Public sector",1,0)

#durmat.healthcareprofessional.assisted - delivery was assisted by a health profession ("accouchement_personnel": A = physician, B = nurse/midwife, C = assistant to the midwife)
women_perinatal_2014$durmat.healthcareprofessional.assisted<-ifelse(women_perinatal_2014$durmat.publichealthcenter.delivery==0,0,
                                                                  ifelse(IHOPE_perinatal_2014$accouchement_personnel_a=="A"| IHOPE_perinatal_2014$accouchement_personnel_b=="B"|IHOPE_perinatal_2014$accouchement_personnel_c=="C",1,0))
women_perinatal_2016$durmat.healthcareprofessional.assisted<-ifelse(women_perinatal_2016$durmat.publichealthcenter.delivery==0,0,
                                                                    ifelse(IHOPE_perinatal_2016$accouchement_personnel_a=="A"| IHOPE_perinatal_2016$accouchement_personnel_b=="B"|IHOPE_perinatal_2016$accouchement_personnel_c=="C",1,0))

#durmat.mother.health.checked - mother's health was checked after delivery ("controle_femme")
women_perinatal_2014$durmat.mother.health.checked<-ifelse(women_perinatal_2014$durmat.publichealthcenter.delivery==0,0,
                                                               ifelse(IHOPE_perinatal_2014$controle_femme=="oui",1,0))
women_perinatal_2016$durmat.mother.health.checked<-ifelse(women_perinatal_2016$durmat.publichealthcenter.delivery==0,0,
                                                          ifelse(IHOPE_perinatal_2016$controle_femme=="oui",1,0))

#durmat.mother.health.checked.professional - mother's health was checked after delivery by a health care professional ("controle_femme_personnel": mÉdecin = physician, "infirmiÈre/sage femme/assistant medical" = nurse, midwife, medical assistant, 
#"accoucheuse traditionnelle formee" = trained traditional midwife, "agent de santÉ communautaire/village" = community health worker)
women_perinatal_2014$durmat.mother.health.checked.professional<-ifelse(women_perinatal_2014$durmat.mother.health.checked==0,0,
                                                                     ifelse(IHOPE_perinatal_2014$controle_femme_personnel=="mÉdecin"|IHOPE_perinatal_2014$controle_femme_personnel=="infirmiÈre/sage femme/assistant medical"|
                                                                              IHOPE_perinatal_2014$controle_femme_personnel=="accoucheuse traditionnelle formee"|IHOPE_perinatal_2014$controle_femme_personnel=="agent de santÉ communautaire/village",1,0))
women_perinatal_2016$durmat.mother.health.checked.professional<-ifelse(women_perinatal_2016$durmat.mother.health.checked==0,0,
                                                                       ifelse(IHOPE_perinatal_2016$controle_femme_personnel=="mÉdecin"|IHOPE_perinatal_2016$controle_femme_personnel=="infirmiÈre/sage femme/assistant medical"|
                                                                                IHOPE_perinatal_2016$controle_femme_personnel=="accoucheuse traditionnelle formee"|IHOPE_perinatal_2016$controle_femme_personnel=="agent de santÉ communautaire/village",1,0))

#durmat.6hourORless.mother.health.checked -  mother's health was checked within 6 hours of delivery("controle_femme_temps_u" = unit of time: "heures"=  hours, "jours" = days, "semaine" = weeks; "controle_femme_temps_d" = length of time )
women_perinatal_2014$durmat.6hourORless.mother.health.checked<-ifelse(women_perinatal_2014$durmat.mother.health.checked==0,0,
                                                                     ifelse(IHOPE_perinatal_2014$controle_femme_temps_u=="nd"|IHOPE_perinatal_2014$controle_femme_temps_u=="jour"|IHOPE_perinatal_2014$controle_femme_temps_u=="semaines",0,
                                                                            ifelse(IHOPE_perinatal_2014$controle_femme_temps_d>6,0,1)))
women_perinatal_2016$durmat.6hourORless.mother.health.checked<-ifelse(women_perinatal_2016$durmat.mother.health.checked==0,0,
                                                                      ifelse(IHOPE_perinatal_2016$controle_femme_temps_u=="nd"|IHOPE_perinatal_2016$controle_femme_temps_u=="jour"|IHOPE_perinatal_2016$controle_femme_temps_u=="semaines",0,
                                                                             ifelse(IHOPE_perinatal_2016$controle_femme_temps_d>6,0,1)))

#durmat.6hourORless.mother.health.checked.professional = mother's health was checked within 6 hours of delivery and by a health care professional
women_perinatal_2014$durmat.6hourORless.mother.health.checked.professional<-ifelse(women_perinatal_2014$durmat.mother.health.checked.professional==0,0,
                                                                     ifelse(IHOPE_perinatal_2014$controle_femme_temps_u=="nd"|IHOPE_perinatal_2014$controle_femme_temps_u=="jour"|IHOPE_perinatal_2014$controle_femme_temps_u=="semaines",0,
                                                                           ifelse(IHOPE_perinatal_2014$controle_femme_temps_d>6,0,1)))
women_perinatal_2016$durmat.6hourORless.mother.health.checked.professional<-ifelse(women_perinatal_2016$durmat.mother.health.checked.professional==0,0,
                                                                                   ifelse(IHOPE_perinatal_2016$controle_femme_temps_u=="nd"|IHOPE_perinatal_2016$controle_femme_temps_u=="jour"|IHOPE_perinatal_2016$controle_femme_temps_u=="semaines",0,
                                                                                          ifelse(IHOPE_perinatal_2016$controle_femme_temps_d>6,0,1)))

#durmat.baby.weighed - baby was weighed after delivery ("accouchement_poids")
women_perinatal_2014$durmat.baby.weighed<-ifelse(women_perinatal_2014$durmat.publichealthcenter.delivery==0,0,
                                               ifelse(IHOPE_perinatal_2014$accouchement_poids=="oui",1,0))
women_perinatal_2016$durmat.baby.weighed<-ifelse(women_perinatal_2016$durmat.publichealthcenter.delivery==0,0,
                                                 ifelse(IHOPE_perinatal_2016$accouchement_poids=="oui",1,0))

#durmat.baby.breastfed - baby was breastfed after delivery ("allaitement")
women_perinatal_2014$durmat.baby.breastfed<-ifelse(women_perinatal_2014$durmat.publichealthcenter.delivery==0,0,
                                                 ifelse(IHOPE_perinatal_2014$allaitement=="oui",1,0))
women_perinatal_2016$durmat.baby.breastfed<-ifelse(women_perinatal_2016$durmat.publichealthcenter.delivery==0,0,
                                                   ifelse(IHOPE_perinatal_2016$allaitement=="oui",1,0))

#durmat.baby.1hourORless.breastfed - baby was breastfed within 1 hour of delivery ("allaitement_temps_u" = unit of time: "immÉdiatement" = immediately  ,"heures"=  hours, "jours" = days, "allaitement_temps_d" = length of time)
women_perinatal_2014$durmat.baby.1hourORless.breastfed<-ifelse(women_perinatal_2014$durmat.baby.breastfed==0,0,
                                                             ifelse(IHOPE_perinatal_2014$allaitement_temps_u=="nd"|IHOPE_perinatal_2014$allaitement_temps_u=="jours",0,
                                                                    ifelse(IHOPE_perinatal_2014$allaitement_temps_u=="heures" & IHOPE_perinatal_2014$allaitement_temps_d>1,0,1)))
women_perinatal_2016$durmat.baby.1hourORless.breastfed<-ifelse(women_perinatal_2016$durmat.baby.breastfed==0,0,
                                                               ifelse(IHOPE_perinatal_2016$allaitement_temps_u=="nd"|IHOPE_perinatal_2016$allaitement_temps_u=="jours",0,
                                                                      ifelse(IHOPE_perinatal_2016$allaitement_temps_u=="heures" & IHOPE_perinatal_2016$allaitement_temps_d>1,0,1)))

#pstmat.baby.health.checked - baby's health was checked after delivery ("controle_enfant")
women_perinatal_2014$pstmat.baby.health.checked<-ifelse(women_perinatal_2014$durmat.publichealthcenter.delivery==0,0,
                                                      ifelse(IHOPE_perinatal_2014$controle_enfant=="oui",1,0))
women_perinatal_2016$pstmat.baby.health.checked<-ifelse(women_perinatal_2016$durmat.publichealthcenter.delivery==0,0,
                                                        ifelse(IHOPE_perinatal_2016$controle_enfant=="oui",1,0))

#pstmat.baby.health.checked.professional - baby's health was checked after delivery by a health care professional ("controle_femme_personnel": mÉdecin = physician, "infirmiÈre/sage femme/assistant medical" = nurse, midwife, medical assistant, 
#"accoucheuse traditionnelle formee" = trained traditional midwife, "agent de santÉ communautaire/village" = community health worker)
women_perinatal_2014$pstmat.baby.health.checked.professional<-ifelse(women_perinatal_2014$pstmat.baby.health.checked==0,0,
                                                                          ifelse(IHOPE_perinatal_2014$controle_enfant_personnel=="mÉdecin"|IHOPE_perinatal_2014$controle_enfant_personnel=="infirmiÈre/sage femme/assistant medical"|
                                                                                   IHOPE_perinatal_2014$controle_enfant_personnel=="accoucheuse traditionnelle formee"|IHOPE_perinatal_2014$controle_enfant_personnel=="agent de santÉ communautaire/village",1,0))
women_perinatal_2016$pstmat.baby.health.checked.professional<-ifelse(women_perinatal_2016$pstmat.baby.health.checked==0,0,
                                                                     ifelse(IHOPE_perinatal_2016$controle_enfant_personnel=="mÉdecin"|IHOPE_perinatal_2016$controle_enfant_personnel=="infirmiÈre/sage femme/assistant medical"|
                                                                              IHOPE_perinatal_2016$controle_enfant_personnel=="accoucheuse traditionnelle formee"|IHOPE_perinatal_2016$controle_enfant_personnel=="agent de santÉ communautaire/village",1,0))

                                                                   
#pstmat.6hourORless.baby.health.checked - baby's health was checked within 6 hours of delivery("controle_enfant_temps_u" = unit of time: "heures"=  hours, "jours" = days, "semaine" = weeks; "controle_enfant_temps_d" = length of time )
women_perinatal_2014$pstmat.6hourORless.baby.health.checked<-ifelse(women_perinatal_2014$pstmat.baby.health.checked==0,0,
                                                                   ifelse(IHOPE_perinatal_2014$controle_enfant_temps_u=="nd"|IHOPE_perinatal_2014$controle_enfant_temps_u=="jours"|IHOPE_perinatal_2014$controle_enfant_temps_u=="semaines",0,
                                                                          ifelse(IHOPE_perinatal_2014$controle_enfant_temps_u=="heures"& IHOPE_perinatal_2014$controle_enfant_temps_d>6,0,1)))
women_perinatal_2016$pstmat.6hourORless.baby.health.checked<-ifelse(women_perinatal_2016$pstmat.baby.health.checked==0,0,
                                                                    ifelse(IHOPE_perinatal_2016$controle_enfant_temps_u=="nd"|IHOPE_perinatal_2016$controle_enfant_temps_u=="jours"|IHOPE_perinatal_2016$controle_enfant_temps_u=="semaines",0,
                                                                           ifelse(IHOPE_perinatal_2016$controle_enfant_temps_u=="heures"& IHOPE_perinatal_2016$controle_enfant_temps_d>6,0,1)))



#pstmat.6hourORless.baby.health.checked.professional - baby's health was checked within 6 hours of delivery and by a health care professional
women_perinatal_2014$pstmat.6hourORless.baby.health.checked.professional<-ifelse(women_perinatal_2014$pstmat.baby.health.checked.professional==0,0,
                                                                  ifelse(IHOPE_perinatal_2014$controle_enfant_temps_u=="nd"|IHOPE_perinatal_2014$controle_enfant_temps_u=="jours"|IHOPE_perinatal_2014$controle_enfant_temps_u=="semaines",0,
                                                                         ifelse(IHOPE_perinatal_2014$controle_enfant_temps_u=="heures"& IHOPE_perinatal_2014$controle_enfant_temps_d>6,0,1)))
women_perinatal_2016$pstmat.6hourORless.baby.health.checked.professional<-ifelse(women_perinatal_2016$pstmat.baby.health.checked.professional==0,0,
                                                                                 ifelse(IHOPE_perinatal_2016$controle_enfant_temps_u=="nd"|IHOPE_perinatal_2016$controle_enfant_temps_u=="jours"|IHOPE_perinatal_2016$controle_enfant_temps_u=="semaines",0,
                                                                                        ifelse(IHOPE_perinatal_2016$controle_enfant_temps_u=="heures"& IHOPE_perinatal_2016$controle_enfant_temps_d>6,0,1)))

#pstmat.baby.noliquids - baby was exclusively breastfed (no other liquids given) for first 3 days of life ("allaitement_exclusif")
women_perinatal_2014$pstmat.baby.noliquids<-ifelse(women_perinatal_2014$durmat.baby.breastfed==0,0,
                                                 ifelse(IHOPE_perinatal_2014$allaitement_exclusif=="non",1,0))
women_perinatal_2016$pstmat.baby.noliquids<-ifelse(women_perinatal_2016$durmat.baby.breastfed==0,0,
                                                   ifelse(IHOPE_perinatal_2016$allaitement_exclusif=="non",1,0))

#pstmat.baby.alive - baby is stil alive
women_perinatal_2014$pstmat.baby.alive<-ifelse(women_perinatal_2014$durmat.publichealthcenter.delivery==0,0,
                                             ifelse(IHOPE_perinatal_2014$enfant_vivant=="vivant",1,0))
women_perinatal_2016$pstmat.baby.alive<-ifelse(women_perinatal_2016$durmat.publichealthcenter.delivery==0,0,
                                               ifelse(IHOPE_perinatal_2016$enfant_vivant=="vivant",1,0))




#Combined 2014 2016 datafame---------------
delivery.care.did=rbind(women_perinatal_2014,women_perinatal_2016)

saveRDS(delivery.care.did,"data/cleaned_perinatal_did.rds")

```

